package mvv.learn.spring.utitilies;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

/**
 * @author Manh Vu
 */
public class AppUtilities {

	public static Map<String, List<?>> splitListIntoMulti(final String key, final Collection<?> c) {
		if(CollectionUtils.isEmpty(c)) {
			return null;
		}

		// prepare key
		String k = key;
		if(StringUtils.isEmpty(k)) {
			k = "key_";
		}

		// process
		List<?> list = new ArrayList<>(c);
		Map<String, List<?>> map = new LinkedHashMap<>();

		int i = 0; // count parameter
		int fromIndex = 0;
		int toIndex = AppParam.VAL_MAX_ELEMENT_SEARCH_IN;

		while (toIndex < list.size()) {
			List<?> l = list.subList(fromIndex, toIndex);

			// put to container and count up
			map.put(k + i++, l);

			// reset index
			fromIndex = toIndex;
			toIndex += AppParam.VAL_MAX_ELEMENT_SEARCH_IN;
		}

		if(fromIndex < list.size()) {
			List<?> l = list.subList(fromIndex, list.size());

			// put to container and count up
			map.put(k + i, l);
		}

		return map;
	}
}
